<?php
namespace app\admin\controller;
use app\admin\model\agentModel;
use app\admin\model\shopModel;
use controller\BasicAdmin;
use think\Db;
class Adminagent extends BasicAdmin{
    public $table = 'agent';

    /**
     * @return mixed
     * 列表
     */
    public function index(){
        list($get, $db) = [$this->request->get(), Db::name($this->table)];
        isset($get['tel']) &&$get['tel'] !='' ?$where ='tel='.trim($get['tel']):$where = '1=1';
        $list = parent::_list($db->where($where)->order('created_at DESC'),1,'','');
        //统计
        $awhere['status'] = 1;
        $awhere['is_real'] = 1;
        isset($get['tel']) &&$get['tel'] !='' ?$awhere['tel'] = trim($get['tel']) : $where = '1=1';
        $all = Db::name($this->table)->where($awhere)->field('count_money,money')->select();
        $list['preMoney'] = 0;
        $list['preCountMoney'] = 0;
        $list['newMoney'] = 0;
        $list['newCountMoney'] =0;
        if(!empty($all) && !empty($list['list'])){
            foreach($list['list'] as $key=>$value){
                $list['preMoney'] += $value['money'];
                $list['preCountMoney'] += $value['count_money'];
            }
            foreach ($all as $k=>$v){
                $list['newMoney'] += $v['money'];
                $list['newCountMoney'] += $v['count_money'];
            }
        }
        return $this->fetch('', ['title' => '代理商列表','list'=>$list]);
    }

    /**
     * 编辑
     */
    public function edit(){
        list($get, $db) = [$this->request->get(), Db::name($this->table)];
        $info = $db->where(['id'=>$get['id']])->find();
        //二维码
        $shopModel = new agentModel();
        $info['recode'] = $shopModel->QRCODE($get['id']);
        return $this->fetch('', ['title' => '编辑代理商','info'=>$info]);
    }

    /**
     * 查看二级代理商
     */
    public function secondaryAgent(){
        list($get, $db) = [$this->request->get(), Db::name($this->table)->alias('a')->join('t_user b','a.id=b.top_agent_id','RIGHT')->join('t_user_role c','b.role_id=c.id','LEFT')];
        $where = '1=1';
        if(isset($get['id'])&&$get['id']!=''){
            $db->where(['a.id'=>$get['id']]);
        }
        $list = parent::_list($db->where($where)->field('b.nickname,b.username,b.mobile,b.role_id,b.created_at,b.status,c.name'),1,'','');
        $list['real_name'] = Db::name($this->table)->where(['id'=>$get['id']])->value('real_name');
        return $this->fetch('', ['title' => '二级代理商列表','list'=>$list]);
    }

    /**
     * 代理商修改
     */
    public function editpost(){
        list($post, $db) = [$this->request->post(), Db::name($this->table)];
        $data['tel'] = $post['tel'];
        $data['real_name'] = $post['real_name'];
        $result = $db->where(['id'=>$post['id']])->update($data);
        if(!empty($result)){
            $this->success('修改成功','');
        }else{
            $this->error('没有数据变化','');
        }
    }

    /**
     * 代理商明细
     */
    public function detailed(){
        $this->table = 'agent_log';
        list($get, $db) = [$this->request->get(), Db::name($this->table)->alias('a')->join('t_user b','a.buid=b.id')->join('t_agent c','a.id=a.aid','LEFT')->field('a.*,b.username,b.mobile,c.real_name')];
        if(isset($get['tel'])&&$get['tel'] !=''){
            $db->where(['b.mobile'=>trim($get['tel'])]);
        }
        $list = parent::_list($db->order('addtime DESC'),1,'','');
        $list['preMoney'] = 0;
        $list['preCountMoney'] = 0;
        $all = Db::name($this->table)->select();
        if(!empty($list) && !empty($all)){
            foreach ($list['list'] as $key=>$value){
                $list['preMoney'] += $value['money'];
            }
            foreach ($all as $val){
                $list['preCountMoney'] += $val['money'];
            }
        }
        return $this->fetch('', ['title' => '代理商明细','list'=>$list]);
    }
}